<?php
/**
 * 账单Service
 * @author solar
 */
class BillService
{
	/**
	 * 创建账单
	 * @author solar
	 * @param int $g_id
	 * @param int $c_id
	 * @param string $note
	 * @param float $amount
	 * @param array $billItemList
	 * @return $b_id
	 */
	public function createBill($g_id,$c_id,$note,$amount,$billItemList) {
		$b_id=ModelFactory::get('BillModel')->createBill($g_id,$c_id,$amount,$note);
		if($b_id){
			$num=ModelFactory::get('BillItemModel')->batchInsert($c_id,$b_id,$billItemList);
			if($num>0){
				/*$checkoutUserModel=ModelFactory::get('CheckoutUserModel');
				$userCheckout=$checkoutUserModel->listByCheckout($c_id);
				foreach($billItemList as $user_id=>$billItem){
					if(isset($userCheckout[$user_id])){
						$updateRow['cu_person_pay'] = $userCheckout[$user_id]['cu_person_pay'] + $billItem['bi_pay'];
						$updateRow['cu_person_spend'] = $userCheckout[$user_id]['cu_person_spend'] + $billItem['bi_consume'];
						$updateRow['cu_balance']=$updateRow['cu_person_pay']-$updateRow['cu_person_spend'];
						$checkoutUserModel->update($updateRow,$userCheckout[$user_id]['cu_id']);
					}else{
						$balance=$billItem['bi_pay']-$billItem['bi_consume'];
						$checkoutUserModel->create($c_id,$user_id,$billItem['bi_pay'],$billItem['bi_consume'],$balance);
					}						
				}
				ModelFactory::get('CheckoutModel')->increaseTotal($c_id,$amount);*/
				$this->computeSummary($c_id);
				return $b_id;
			}			
		}
		return false;
	}	
	
	/**
	 * 修正误差，让支出总金额和消费总金额保持一致
	 * @author solar
	 * @param float $balance
	 * @param array $billItemList
	 * @return void
	 */
	public function fixBillItems($balance,&$billItemList) {
		if($balance==0) return;
		$aUserID=array_keys($billItemList);
		$me=CoreFactory::session()->get('user_id');
		if($balance<0) $user_id=in_array($me,$aUserID) ? $me : array_rand($aUserID);
		else if($balance>0){
			if(count($aUserID)>1){
				$key=array_search($me,$aUserID);
				if($key!==false) unset($aUserID[$key]);
			}
			$user_id=array_rand($aUserID);
		}
		$billItemList[$user_id]['bi_consume']+=$balance;		
	}
	
	/**
	 * 删除记账
	 * @author solar
	 * @param int $bill_id
	 * @return boolean
	 */
	public function removeBill($bill_id) {
		$num = ModelFactory::get('BillItemModel')->removeByField(array('b_id'=>$bill_id));
		if($num > 0){
			$result = ModelFactory::get('BillModel')->remove($bill_id);
			if($result) return true;
		}
		return false;
	}
	
	/**
	 * 计算并更新账单汇总金额
	 * @author solar
	 * @param int $checkout_id
	 */
	public function computeSummary($checkout_id) {
		$total = 0;
		$statList = ModelFactory::get('BillItemModel')->statUser($checkout_id);
		foreach($statList as $statRow){
			$aCheckoutUser['cu_person_pay'] = $statRow['pay'];
			$aCheckoutUser['cu_person_spend'] = $statRow['consume'];
			$aCheckoutUser['cu_balance'] = $statRow['pay']-$statRow['consume'];
			$checkoutUserModel = ModelFactory::get('CheckoutUserModel');
			$row = $checkoutUserModel->getCheckoutUser($statRow['user_id'], $checkout_id);
			if(empty($row)){
				$aCheckoutUser['c_id'] = $checkout_id;
				$aCheckoutUser['u_id'] = $statRow['user_id'];
				$checkoutUserModel->insert($aCheckoutUser);
			}else{
				$checkoutUserModel->update($aCheckoutUser,$row['cu_id']);
			}
			$total += $statRow['pay'];
		}
		ModelFactory::get('CheckoutModel')->update(array('c_total'=>$total), $checkout_id);
	}
	
	/**
	 * 检查成员是否在组的未结账单中
	 * @author solar
	 * @param int $user_id
	 * @param int $group_id
	 * @return boolean
	 */
	public function inUncheckoutBill($user_id,$group_id) {
		$checkoutRow=ModelFactory::get('CheckoutModel')->getUncheckout($group_id);
		if(!$checkoutRow) return false;
		$aCheckoutUser=ModelFactory::get('CheckoutUserModel')->getCheckoutUser($user_id,$checkoutRow['c_id']);
		return $aCheckoutUser ? true : false;
	}
}
